package com.example.demo.jacob;


import org.apache.commons.io.IOUtils;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public class Test3 {
    private static final String DEFAULT_CONVERTER_CHARSET = System.getProperty("sun.jnu.encoding");
    public static void main(String[] args) throws Exception {
//        test2();
        test1();

    }
    public static void test2()throws Exception{
        //        File file=new File("E:\\06_xisoft\\1692857448869@报销费用明细页签.xlsx");
//        File file=new File("F:\\360MoveData\\Users\\lqq\\Desktop\\image\\系统端口梳理.xlsx");
        File file = new File("F:\\360MoveData\\Users\\lqq\\Desktop\\image\\bx\\gbk.txt");
//File file=new File("F:\\360MoveData\\Users\\lqq\\Desktop\\image\\bx\\1.xlsx");
//File file=new File("F:\\360MoveData\\Users\\lqq\\Desktop\\image\\bx\\报销费用明细页签.xlsx");
//        File file = new File("F:\\360MoveData\\Users\\lqq\\Desktop\\image\\测试.xlsx");
        InputStream inputStream = new FileInputStream(file);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, DEFAULT_CONVERTER_CHARSET));
        String line;
        StringBuilder sb = new StringBuilder();
        try {
            for(; null != (line = reader.readLine()); sb.append(line)) {
                if (line.contains("charset=gb2312")) {
                    line = line.replace("charset=gb2312", "charset=utf-8");
                }
            }
        } catch (Throwable var91) {
            throw var91;
        }
    }
    public  static void test1()throws Exception{
        //        File file=new File("E:\\06_xisoft\\1692857448869@报销费用明细页签.xlsx");
//        File file=new File("F:\\360MoveData\\Users\\lqq\\Desktop\\image\\系统端口梳理.xlsx");
//        File file = new File("F:\\360MoveData\\Users\\lqq\\Desktop\\image\\bx\\gbk.txt");
//File file=new File("F:\\360MoveData\\Users\\lqq\\Desktop\\image\\bx\\1.xlsx");
//File file=new File("F:\\360MoveData\\Users\\lqq\\Desktop\\image\\bx\\报销费用明细页签.xlsx");
//        File file = new File("F:\\360MoveData\\Users\\lqq\\Desktop\\image\\测试.xlsx");
        File file = new File("E:\\06_xisoft\\02_code\\frame-service\\fileManager\\sf\\fjfile\\1693653663655@人员银行信息 (8).html");
        String charset=GetEncoding(file);
        System.out.println(file.getName()+"字符集为："+charset);
        InputStream inputStream=new FileInputStream(file);
        List<String> lines = IOUtils.readLines(inputStream,charset);
        for (String line:lines){
            System.out.println(line);
        }
    }
    public static String GetEncoding(File file) {
        String charset = "GBK";
        byte[] first3Bytes = new byte[3];
        try {
            boolean checked = false;
            InputStream is = new FileInputStream(file);
            int read = is.read(first3Bytes, 0, 3);

            if (read == -1)
                return charset;
            if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
                charset = "UTF-16LE";
                checked = true;
            } else if (first3Bytes[0] == (byte) 0xFE
                    && first3Bytes[1] == (byte) 0xFF) {
                charset = "UTF-16BE";
                checked = true;
            } else if (first3Bytes[0] == (byte) 0xEF
                    && first3Bytes[1] == (byte) 0xBB
                    && first3Bytes[2] == (byte) 0xBF) {
                charset = "UTF-8";
                checked = true;
            } else if (first3Bytes[0] == (byte) 0xA
                    && first3Bytes[1] == (byte) 0x5B
                    && first3Bytes[2] == (byte) 0x30) {
                charset = "UTF-8";
                checked = true;
            } else if (first3Bytes[0] == (byte) 0xD
                    && first3Bytes[1] == (byte) 0xA
                    && first3Bytes[2] == (byte) 0x5B) {
                charset = "GBK";
                checked = true;
            } else if (first3Bytes[0] == (byte) 0x5B
                    && first3Bytes[1] == (byte) 0x54
                    && first3Bytes[2] == (byte) 0x49) {
                charset = "windows-1251";
                checked = true;
            }
            //bis.reset();
            InputStream istmp = new FileInputStream(file);
            if (!checked) {
                int loc = 0;
                while ((read = istmp.read()) != -1) {
                    loc++;
                    if (read >= 0xF0)
                        break;
                    if (0x80 <= read && read <= 0xBF)
                        break;
                    if (0xC0 <= read && read <= 0xDF) {
                        read = istmp.read();
                        if (0x80 <= read && read <= 0xBF)
                            continue;
                        else
                            break;
                    } else if (0xE0 <= read && read <= 0xEF) {
                        read = istmp.read();
                        if (0x80 <= read && read <= 0xBF) {
                            read = istmp.read();
                            if (0x80 <= read && read <= 0xBF) {
                                charset = "UTF-8";
                                break;
                            } else
                                break;
                        } else
                            break;
                    }
                }
            }
            is.close();
            istmp.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return charset;
    }
    public static boolean isImgeOrPdf(String suffixName) {
        String[] picTypelist = {"jpg","png","jpeg","bmp","tif","gif","psd","wmf", "dxf", "pcd", "cdr","fpx","tga","pcx","pdf","heif","heic"};//图片格式
        for (String picType:picTypelist) {
            if(suffixName.toLowerCase().indexOf(picType)>=0){
                return true;
            }
        }
        return false;
    }
}
